package com.gzdh.tang.modu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.gzdh.tang.dishrecognition.dto.ReserveListDto;
import com.gzdh.tang.modu.entity.Reserve;
import com.gzdh.tang.modu.entity.ReserveTongji;
import com.gzdh.tang.modu.model.OrderModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;


public interface ReserveMapper extends BaseMapper<Reserve> {

    @Select("select SUM(je) as price," +
            "COUNT(*) as counts, " +
            "cs from RESERVE " +
            "where CZRQ>#{czrq} and " +
            "SDDZ=#{sddz} " +
            "group by cs")
    List<ReserveTongji> getReserve(@Param("sddz") String sddz, @Param("czrq") String czrq);


    @Select("select top 20  je as price,kehu as name,convert(varchar(100),CZRQ,8) as pay_time" +
            " from RESERVE " +
            "where  CZRQ>#{czrq} and  " +
            "SDDZ=#{sddz} order by czrq desc")
    List<OrderModel> getOrderModel(@Param("sddz") String sddz, @Param("czrq") String czrq);


    @Select("select ZFSerialNumber from RESERVE where cardno=#{cardno} and dinner=#{dinner} " +
            "and DATEDIFF(dd,dcrq,getDate())=0 and cs=0 group by ZFSerialNumber")
    List<String> payCounts(@Param("cardno") String cardno, @Param("dinner") String dinner);


//    @Select("select * from (" +
//            "SELECT  zfserialnumber,kehu,SUM(JE) as je,MAX(czrq) as czrq ,cs,sc" +
//            "  FROM RESERVE_xfj where je>0 and  czrq >=#{startTime} and czrq<=#{endTime} and sddz=#{deviceId} " +
//            "  group by zfserialnumber,kehu ,cs,sc )as t order by t.CZRQ desc")
    List<ReserveListDto> getReserve(@Param("startTime") String startTime, @Param("endTime") String endTime,
                                    @Param("deviceId") String deviceId);

    @Select("exec checkDingdan @bqh=#{bqh}, @ksh=#{ksh},@begintime=#{startTime},@endtime=#{endTime}")
    List<Map<String,Object>> getDiningTableList(@Param("startTime") String startTime, @Param("endTime") String endTime,
                            @Param("bqh") String bqh, @Param("ksh") String ksh);

    @Select("exec checkDingdan_fantai @bqh=#{bqh}, @ksh=#{ksh},@begintime=#{startTime},@endtime=#{endTime}")
    List<Map<String,Object>> getCleanedDiningTableList(@Param("startTime") String startTime, @Param("endTime") String endTime,
                                                @Param("bqh") String bqh, @Param("ksh") String ksh);


    @Update("update RESERVE_xfj set cs=1,DCRQ = replace(CONVERT(varchar(100),[DCRQ], 25),'.000','.127') where zfserialnumber=#{orders}")
    int updateRESERVEXfj(@Param("orders") String orders);

    @Update("update RESERVE set cs=1,DCRQ = replace(CONVERT(varchar(100),[DCRQ], 25),'.000','.127') where zfserialnumber=#{orders}")
    int updateRESERV(@Param("orders") String orders);


    @Select(" select sum(je) as price from RESERVE where cardno=#{cardno} and dcrq =#{dcrq} and dinner=#{dinner} and cs=0")
    String getPriceByReserve(@Param("cardno")String cardno,@Param("dcrq")String dcrq,@Param("dinner")String dinner);

    @Select("select zfserialnumber from RESERVE where CARDNO=#{cardno} and DINNER=#{dinner} and DCRQ=#{dcrq} and cs=0 and status=#{status} group by zfserialnumber")
    List<Map<String, Object>> countReserves(@Param("cardno") int cardno, @Param("dinner") String dinner, @Param("dcrq") String dcrq, @Param("status")int status);

    @Update("update RESERVE set status=1 where CARDNO=#{cardno} and DINNER=#{dinner} and DCRQ=#{dcrq} and cs=0 ")
    int updateStatus(@Param("cardno") int cardno, @Param("dinner") String dinner, @Param("dcrq") String dcrq);

    @Select("select hName,je,sl from reserve where (str1 = 0 or str1 is null) and ksh=#{ksh} " +
            "and dinner=#{dinner} and dcRq = #{date}")
    List<Reserve> getDiningTableDetails(@Param("ksh")String ksh,
                                        @Param("dinner")String dinner,
                                        @Param("date")String date);

    @Select("select hName,je,sl from reserve where str1=#{useTimes} and ksh=#{ksh} " +
            "and dinner=#{dinner} and dcRq = #{date}")
    List<Reserve> getCleanedTableDetails(@Param("ksh")String ksh,
                                         @Param("dinner")String dinner,
                                         @Param("date")String date,
                                         @Param("useTimes") String useTimes);

    @Select("select max(str1) from reserve where ksh=#{ksh} and dcRq=#{dcTime} and dinner=#{dinner}")
    String getMaxUseTimes(Map<String, Object> params);

    @Update("update reserve set str1 = #{times} " +
            "where (str1 = 0 or str1 is null) and ksh=#{ksh} and dcRq=#{dcTime} and dinner=#{dinner}")
    Integer updateUseTimes(Map<String, Object> params);
}
